package com.neuedu.javase.collections;

import java.util.*;

/**
 * @auth 金山老师
 * title:   书籍代码
 * creatime: 2022-09-30 8:43
 * 描述 :
 */
public class MyArrayList {


    public static void main(String[] args) {

//        创建对象
//        List list = new ArrayList();
//        List list = new ArrayList(16);
//        System.out.println("list.size() = " + list.size());



        //使用集合初始化 ArrayList
        Collection collection = new HashSet();

        collection.add("100");
        collection.add("200");
        collection.add("300");
        collection.add(new String("300"));

        
        List list = new ArrayList(collection);
        System.out.println("list.size() = " + list.size());  //3
        System.out.println("==================================");

        //使用集合初始化 ArrayList
        Collection collection2 = new ArrayList();

        collection2.add("110");
        collection2.add("220");
        collection2.add("330");
        collection2.add(new String("330"));


        List list2 = new ArrayList(collection2);
        System.out.println("list2.size() = " + list2.size());  //4


        //常用的方法
        System.out.println("list2.get(0) = " + list2.get(0));
        System.out.println("list2.get(1) = " + list2.get(1));


        System.out.println("====================遍历====================");
        //遍历元素   存储的顺序 和 遍历的顺序一直
        for (int i = 0; i < list2.size(); i++) {
            System.out.println("list2.get("+i+") = " + list2.get(i));
        }



        //迭代器的方式遍历
       //获取迭代器
        for( Iterator it = list2.iterator();it.hasNext();){
            Object obj = it.next();
            System.out.println("for ite==obj = " + obj);
        }


        //迭代器的方式遍历
        Iterator it = list2.iterator();//获取迭代器
        while(it.hasNext()){
            Object obj = it.next();
            System.out.println("while ite==obj = " + obj);
        }

        for (Object o : list2) {
            System.out.println("增强for==o = " + o);
        }



        List myList = new ArrayList();
        myList.add(100);  //  add(Integer(100))
        myList.add(200);
        myList.add(300);   //追加
        myList.add(1,100000); //插入

//        myList.add(list2);  // [100, 100000, 200, 300, [110, 220, 330, 330]]
        myList.addAll(list2);

        System.out.println("myList = " + myList);
        System.out.println("myList.size() = " + myList.size());

        System.out.println("myList.contains(new Integer(100)) = " + myList.contains(new Integer(100)));

        System.out.println("myList.isEmpty() = " + myList.isEmpty());


        //是否包含另外一个集合
        System.out.println("myList.containsAll(list2) = " + myList.containsAll(list2));

        // [100, 100000, 200, 300, 110, 220, 330, 330   。。。。。。。。。。。。。。。]
        System.out.println("myList.indexOf(200) = " + myList.indexOf(200));  // 2
        System.out.println("myList.indexOf(30) = " + myList.indexOf(30));  // -1

        Object removeObject = myList.remove(1);
        System.out.println("removeObject = " + removeObject); //100000

        System.out.println("myList = " + myList);
        System.out.println("myList.size() = " + myList.size());

        System.out.println("myList.remove(new Integer(200)) = " + myList.remove(new Integer(200)));
        System.out.println("myList = " + myList);
        System.out.println("myList.size() = " + myList.size());

        List subList = myList.subList(0, 3);
        System.out.println("subList.getClass() = " + subList.getClass());
        System.out.println("subList = " + subList);

    }






}
